package com.dayatang.android.Adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Activity.WorkloadActivity;
import com.dayatang.android.config.Config;

public class PMOProjectStatusListViewAdapter extends BaseAdapter {
	
	private Context context;
	private List<HashMap<String, Object>> listItem;
	private LayoutInflater listContainer;           

	class ListItemView{
		private TextView projnameTextView;
		private TextView receiptTextView;
		private TextView progressTextView;
		private TextView expenditureTextView;
		private TextView outputTextView;
		private Button workloadButton;
	}
	
	public PMOProjectStatusListViewAdapter(Context context, List<HashMap<String, Object>> listItem) {
		this.listItem = listItem;
		this.context = context;
		listContainer = LayoutInflater.from(context);  
	}

	public int getCount() {
		return listItem.size();
	}

	public Object getItem(int position) {
		return listItem.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ListItemView  listItemView = null;
		
		if(convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(R.layout.pmo_projectstatus_listview_item, null);
			
			listItemView.projnameTextView = (TextView) convertView.findViewById(R.id.pmo_projectstatus_projectname);
			listItemView.receiptTextView = (TextView) convertView.findViewById(R.id.pmo_projectstatus_receipt);
			listItemView.progressTextView = (TextView) convertView.findViewById(R.id.pmo_projectstatus_progress);
			listItemView.expenditureTextView = (TextView) convertView.findViewById(R.id.pmo_projectstatus_expenditure);
			listItemView.outputTextView = (TextView) convertView.findViewById(R.id.pmo_projectstatus_output);
			listItemView.workloadButton = (Button) convertView.findViewById(R.id.pmo_projectstatus_workload);
			
            convertView.setTag(listItemView);    
		} else {    
            listItemView = (ListItemView)convertView.getTag();    
        }  
		
		listItemView.receiptTextView.setText((String)listItem.get(position).get("ItemProjectReceipt"));
		listItemView.progressTextView.setText((String)listItem.get(position).get("ItemProjectStatus"));
		listItemView.expenditureTextView.setText((String)listItem.get(position).get("ItemProjectExpenditure"));
		listItemView.outputTextView.setText((String)listItem.get(position).get("ItemProjectOutput"));
		listItemView.projnameTextView.setText((String)listItem.get(position).get("ItemProjectName"));
		final String projectid = (String)listItem.get(position).get("ItemProjectId");
		
		listItemView.workloadButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(context,WorkloadActivity.class);
				intent.putExtra(Config.PROJID, projectid);
				context.startActivity(intent);
			}
		});
		
		return convertView;
	}

}
